package org.rxc.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.rxc.base.annotation.PlatformAnnotation;
import org.rxc.org.domain.Employee;
import org.rxc.org.query.EmployeeQuery;
import org.rxc.org.service.IEmployeeService;
import org.rxc.base.utils.AjaxResult;
import org.rxc.base.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的相关文档",description = "员工相关的CRUD功能")
@PlatformAnnotation(name = "员工管理")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    //添加和修改写一个接口
    //@RequestMapping(method = RequestMethod.PUT)
    @PutMapping
    @ApiOperation(value = "添加或修改数据")
    @PlatformAnnotation(name = "员工的添加或修改")
    public AjaxResult saveOrUpdate(@RequestBody @ApiParam(value ="id为空为新增数据,id不为空则修改数据") Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败: "+e.getMessage());
        }

    }

    //    @RequestMapping("/delete")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除数据")
    @PlatformAnnotation(name = "删除员工")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败: "+e.getMessage());
        }
    }


    //    @RequestMapping("/queryById")
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单条数据")
    @PlatformAnnotation(name = "查询员工单条")
    public Employee queryById(@PathVariable("id") Long id){
        try {
            return  employeeService.queryById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //    @RequestMapping("/queryAll")
    @GetMapping
    @ApiOperation(value = "查询全部数据")
    @PlatformAnnotation(name = "查询员工全部")
    public List<Employee> queryAll(){
        try {
            return employeeService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping
    @PlatformAnnotation(name = "分页查询")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        try {
            return employeeService.queryPage(employeeQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     *  方案一:
     *     for(Long id :ids){
     *         employeeService.deleteById(id)
     *     }
     *
     *   方案二：
     *     ids: [31, 33]
     *       select * from t_org_employee where id in(31, 33)
     *
     *       delete from t_org_employee where id in(31, 33)
     *
     *       动态sql foreach
     *
     * */

    @PatchMapping
    @ApiOperation(value = "批量删除数据")
    @PlatformAnnotation(name = "批量删除")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败: "+e.getMessage());
        }
    }
    
}
